﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;



namespace VodafoneLiveTile.Connections
{
    public class HTMLPaser
    {
        char[] html;
        public int pointer;

        public HTMLPaser(string html)
        {
            this.html = html.ToCharArray();
            pointer = 0;


        }



        protected bool GoToEndOfString(string searched)
        {
            if (searched.Length <= 0) return false;
            if (pointer >= html.Length) return false; //throw new InvalidRequestException();
            char[] s = searched.ToCharArray();

            for (int i = pointer; i < html.Length - s.Length; i++)
            {
                if (html[i] != s[0]) continue;
                bool found = true;
                for (int y = 1; y < s.Length; y++)
                    if (html[y + i] != s[y])
                    {
                        found = false;
                        break;
                    }
                if (found)
                {
                    //offset = i;
                    pointer = i + s.Length;

                    return true;
                }
            }

            return false;
        }

        protected string GetTextUntilNextChar(char c)
        {
            string s = "";
            bool skip_spaces = true;
            for (int i = pointer; i < html.Length && html[i] != c; i++)
            {
                if (!skip_spaces || (html[i] != ' ' && html[i] != '\n' && html[i] != '\r'))
                {
                    skip_spaces = false;
                    s += html[i];
                }
            }
            pointer += s.Length;
            return s;
        }

        protected int SearchNumberOfChanges()
        {
            char[] test = "<td rowspan=\"".ToCharArray();
            pointer++;
            for (int i = 0; i < test.Length; i++)
            {
                var a = html[pointer + i];
                if (html[pointer + i] != test[i])
                {
                    pointer += 4;
                    return 0;
                }
            }
            pointer += test.Length;
            var b = html[pointer];
            //int rows = Int32.Parse(""+a);
            int rows = Int32.Parse("" + b);
            pointer += 3;
            return rows;
        }

        protected bool IsStringPresentFromPointer(int startPointer, int stopPointer, string searched)
        {
            char[] s = searched.ToCharArray();
            //bool found = false;
            //for (int i = startPointer; i < html.Length - s.Length; i++)
            //{
            //    for (int y = 0; y < s.Length; y++)
            //    {
            //        if (html[i + y] != s[y])
            //            break;
            //    }
            //}
            //return found;

            //for (int i = pointer; i < html.Length - s.Length; i++)
            var stop = Math.Min(html.Length - s.Length, stopPointer);
            for (int i = startPointer; i < stop; i++)
            {
                if (html[i] != s[0]) continue;
                bool found = true;
                for (int y = 1; y < s.Length; y++)
                    if (html[y + i] != s[y])
                    {
                        found = false;
                        break;
                    }
                if (found)
                    return true;
            }
            return false;
        }

        protected bool IsStringPresentFromPointer(int startPointer, string searched)
        {
            char[] s = searched.ToCharArray();
            //bool found = false;
            //for (int i = startPointer; i < html.Length - s.Length; i++)
            //{
            //    for (int y = 0; y < s.Length; y++)
            //    {
            //        if (html[i + y] != s[y])
            //            break;
            //    }
            //}
            //return found;

            //for (int i = pointer; i < html.Length - s.Length; i++)
            for (int i = startPointer; i <= html.Length - s.Length; i++)
            {
                if (html[i] != s[0]) continue;
                bool found = true;
                for (int y = 1; y < s.Length; y++)
                    if (html[y + i] != s[y])
                    {
                        found = false;
                        break;
                    }
                if (found)
                    return true;
            }
            return false;
        }
        public bool IsStringPresentFromStart(string searched)
        {
            return IsStringPresentFromPointer(0, searched);
        }




        protected int GetStringPointer(string searched)
        {
            char[] s = searched.ToCharArray();
            for (int i = pointer; i < html.Length - s.Length; i++)
            {
                if (html[i] != s[0]) continue;
                bool found = true;
                for (int y = 1; y < s.Length; y++)
                    if (html[y + i] != s[y])
                    {
                        found = false;
                        break;
                    }
                if (found)
                    return i;
            }
            return -1;
        }
    }
    public class PhoneNumbersParser : HTMLPaser
    {
        public PhoneNumbersParser(string html)
            : base(html)
        {
        }

        public List<String> getNumbers()
        {
            List<string> numbers = new List<string>();
            //GoToEndOfString("score: ");
            //GoToEndOfString("<table><tr><td>");

            while (GoToEndOfString("<option id=\"S\" value=\""))
            {
                numbers.Add(GetTextUntilNextChar('"'));
            }

            return numbers;
        }
    }

    public class MobileInternetParser : HTMLPaser
    {
        public MobileInternetParser(string html)
            : base(html)
        {
        }

        public String getResult()
        {

            //GoToEndOfString("score: ");
            //GoToEndOfString("<table><tr><td>");
            pointer = 0;
            if (!GoToEndOfString("<td class=\"tdOdd left\">"))
                return "";
            if (!GoToEndOfString("<td class=\"tdOdd left\">"))
                return "";
            var s1 = GetTextUntilNextChar('<');
            if (!GoToEndOfString("<"))
                return "";
            if (!GoToEndOfString("<td class=\"tdOdd left\">"))
                return "";
            if (!GoToEndOfString("<td class=\"tdOdd left\">"))
                return "";
            var s2 = GetTextUntilNextChar('<');

            try
            {
                double i1 = Double.Parse(s1);
                double i2 = Double.Parse(s2);
                i1 = Math.Round(i1 / 1024, 1);
                i2 = Math.Round(i2 / 1024);

                return "" + i1 + " / " + i2 + " MB";
            }
            catch
            {
                return "";
            }


        }
    }
    public class SmartZeroLimitsParser : HTMLPaser
    {
        public SmartZeroLimitsParser(string html)
            : base(html)
        {
        }

        public String getResult()
        {

            //GoToEndOfString("score: ");
            //GoToEndOfString("<table><tr><td>");
            pointer = 0;
            if (!GoToEndOfString("<td class=\"tdOdd left\">"))
                return "";
            if (!GoToEndOfString("<td class=\"tdOdd left\">"))
                return "";
            var s1 = GetTextUntilNextChar('<');
            if (!GoToEndOfString("<"))
                return "";
            if (!GoToEndOfString("<td class=\"tdOdd left\">"))
                return "";
            if (!GoToEndOfString("<td class=\"tdOdd left\">"))
                return "";
            var s2 = GetTextUntilNextChar('<');

            try
            {
                double i1 = Parse(s1);
                double i2 = Parse(s2);
                i1 = Math.Round(i1, 1);
                i2 = Math.Round(i2);

                return "" + i1 + " / " + i2 + " MB";
            }
            catch
            {
                return "";
            }


        }
        public double Parse(string str)
        {
            var splitted = str.Split('.');
            if (splitted.Length == 0)
                return 0;
            if (splitted.Length == 1)
                return Double.Parse(splitted[0]);
            if (splitted.Length > 2)
                return 0;
            else
            {
                var i = Double.Parse(splitted[0]);
                var d = Double.Parse(splitted[1]);

                var divisor = 1;
                for (int j = 0; j < splitted[1].Length; j++)
                    divisor *= 10;

                return i + d / divisor;
            }
        }
    }
    /*public class AccountsParser : HTMLPaser
    {
        public AccountsParser(string html)
            : base(html)
        {
        }

        public List<Account> getAccounts()
        {
            List<Account> accounts = new List<Account>();

            while (GoToEndOfString("<tr>"))
            {
                GoToEndOfString("<td>");
                GoToEndOfString("<td>");
                Account c = new Account();
                c.username = GetTextUntilNextChar('<');
                GoToEndOfString("<td>");
                c.password = GetTextUntilNextChar('<');
                GoToEndOfString("<td>");
                c.number = GetTextUntilNextChar('<');
                GoToEndOfString("<td>");
                var b = GetTextUntilNextChar('<');
                if (b == "1")
                    c.othersNumbers.Add(c.number);
                else
                    c.othersNumbers.Add("");
                GoToEndOfString("<td>");
                b = GetTextUntilNextChar('<');
                if (b == "1")
                    c.showDate = true;
                else
                    c.showDate = false;
                GoToEndOfString("<td>");
                c.tileUri = GetTextUntilNextChar('<');
                GoToEndOfString("<td>");
                c.toastUri = GetTextUntilNextChar('<');
                GoToEndOfString("<td>");
                GoToEndOfString("<td>");
                b = GetTextUntilNextChar('<');
                c.counter = Utils.GetType(b);
                accounts.Add(c);
            }

            return accounts;
        }
    }*/

    public class CountersParser : HTMLPaser
    {
        public CountersParser(string html)
            : base(html)
        {
        }

        public List<Counter> getCounters()
        {
            List<Counter> counters = new List<Counter>();

            while (GoToEndOfString("<tr>"))
            {
                GoToEndOfString("<td>");
                Counter c = new Counter();
                c.Id = Int32.Parse(GetTextUntilNextChar('<'));
                GoToEndOfString("<td>");
                c.Name = GetTextUntilNextChar('<');

                counters.Add(c);
            }

            return counters;
        }
    }
    public class ToastsParser : HTMLPaser
    {
        public ToastsParser(string html)
            : base(html)
        {
        }

        public List<ToastViewModel> getCounters()
        {
            List<ToastViewModel> toasts = new List<ToastViewModel>();

            while (GoToEndOfString("<tr>"))
            {
                GoToEndOfString("<td>");
                ToastViewModel c = new ToastViewModel();
                c.datetime = GetTextUntilNextChar('<');
                GoToEndOfString("<td>");
                c.row1 = GetTextUntilNextChar('<');
                GoToEndOfString("<td>");
                c.row2 = GetTextUntilNextChar('<');
                toasts.Add(c);
            }

            return toasts;
        }
    }
    public class AdsParser : HTMLPaser
    {
        public AdsParser(string html)
            : base(html)
        {
        }

        public List<Ads> getAds()
        {
            List<Ads> Ads = new List<Ads>();

            while (GoToEndOfString("<tr>"))
            {
                GoToEndOfString("<td>");
                Ads c = new Ads();
                c.ID = GetTextUntilNextChar('<');
                GoToEndOfString("<td>");
                c.Name = GetTextUntilNextChar('<');
                GoToEndOfString("<td>");
                c.ImageUrl = GetTextUntilNextChar('<');
                GoToEndOfString("<td>");
                c.Description = GetTextUntilNextChar('<');
                Ads.Add(c);
            }

            return Ads;
        }
    }
}
